package com.soft.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

import com.soft.admin.common.ElementConst;
import com.soft.dao.ChannelDao;
import com.soft.front.dto.ChannelDTO;
import com.soft.model.Channel;

@SuppressWarnings("unchecked")
@Component
public class ChannelDaoImpl extends BaseDaoImpl implements ChannelDao {
	
	@Cacheable(value = "CHANNEL_INFO_CACHE", key="#namespace + 'get'")
	public Channel get(String namespace) {
		return (Channel)this.findUniqueByProperty(Channel.class, "namespace", namespace);
	}

	@Cacheable(value = "CHANNEL_INFO_CACHE", key="#namespace + 'getLst'")
	public List<ChannelDTO> getLst(String namespace) {
		Channel chan = this.get(namespace);
		List<Channel> channels = this.findAll(Channel.class, new Order[]{Order.desc("level"), Order.asc("id")}, 
				Restrictions.eq("parent.id", chan.getId()));
		List<ChannelDTO> channelDTO = new ArrayList<ChannelDTO>();
		for (Channel channel : channels) {
			ChannelDTO dto = new ChannelDTO();
			BeanUtils.copyProperties(channel, dto);
			dto.setUrl(namespace + "/" + channel.getNamespace() + "/index.jhtml");
			channelDTO.add(dto);
		}
		return channelDTO;
	}
}
